package br.com.gcl.dao.impl;

import java.sql.Connection;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;

import org.apache.log4j.Logger;

import br.com.gcl.dao.*;

public class SQLDAOFactory extends DAOFactory {
	
	public static Logger logger = Logger.getLogger(SQLDAOFactory.class);
	
	private Connection getConn() throws Exception {
		Connection connection = null;
		
		//Class.forName("com.mysql.jdbc.Driver").newInstance();
		
		//connection = DriverManager.getConnection("jdbc:mysql://mysql01.moradadosol.tur.br/moradadosol?user=moradadosol&password=rfg21061983");
		//connection = DriverManager.getConnection("jdbc:mysql://localhost/moradadosol?user=root&password=root");
		
		//logger.setLevel(Constantes.LOGGER_LEVEL);
		
		try {
			
			Context ctx = (Context) new InitialContext().lookup("java:comp/env");
			DataSource dataSource = (DataSource)ctx.lookup("jdbc/cadlut");
			
			connection = dataSource.getConnection();
			
			System.out.println("Conectado: " + (connection!=null));
			
		} catch (Exception e) {
			logger.error("Erro ao obter SQLDAOFactory", e);
			throw e;
		}
		return connection;
	}

	@Override
	public ComunidadeDAO getComunidadeDAO() throws Exception {
		return new ComunidadeSqlDao(getConn());
	}

	@Override
	public EnderecoDAO getEnderecoDAO() throws Exception {
		return new EnderecoSqlDAO(getConn());
	}

	@Override
	public UsersDAO getUserDAO() throws Exception {
		return new UserSqlDao(getConn());
	}
	
	
	
}
